package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.tooling.preview.Preview

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = Color.Transparent // 设置为透明，让渐变背景显示
                ) {
                    GradientBackgroundWithContent()
                }
            }
        }
    }
}

@Composable
fun GradientBackgroundWithContent() {
    // 创建从上到下（红色到白色）的线性渐变笔刷
    val gradientBrush = Brush.verticalGradient(
        colors = listOf(
            Color(0xFFF15656), // 红色
            Color(0xFFF1E5E5)  // 白色
        ),
        startY = 0f, // 从顶部开始
        endY = Float.POSITIVE_INFINITY // 到底部结束
    )

    // 使用Box布局应用渐变背景
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(brush = gradientBrush), // 应用渐变背景
        contentAlignment = Alignment.Center
    ) {
        Greeting(
            name = "谁都不能例外",
            modifier = Modifier.fillMaxSize()
        )
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    val mainImg = painterResource(id = R.drawable.ic_launcher_foreground_png)

    // 为每个联系信息项使用不同的自定义图标
    val phoneIcon = painterResource(id = R.drawable.oip_c) // 自定义手机图标
    val emailIcon = painterResource(id = R.drawable.emil) // 自定义邮箱图标
    val usernameIcon = painterResource(id = R.drawable.oip_d) // 自定义用户图标

    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier.fillMaxSize()
    ) {
        Spacer(modifier = Modifier.height(50.dp))

        // 上半部分：标题区域
        Column(
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth()
        ) {
            Image(
                painter = mainImg,
                contentDescription = "应用主图标",
                modifier = Modifier.padding(vertical = 10.dp)
            )
            Text(
                text = "散伙是人间常态,$name",
                fontSize = 20.sp,
                color = Color.Black, // 确保文字在红色背景上可读
                modifier = Modifier.padding(vertical = 5.dp)
            )
            Text(
                text = "若我死去,你可以好友去世为由请一天假，但不必来，好好休息",
                fontSize = 13.sp,
                color = Color.Black, // 确保文字在红色背景上可读
                modifier = Modifier.padding(vertical = 5.dp)
            )
        }

        Spacer(modifier = Modifier.height(30.dp))

        // 下半部分：联系信息（使用自定义图标）
        Column(
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.Top,
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 60.dp, top = 0.dp, bottom = 40.dp)
        ) {
            // 手机号 - 使用自定义手机图标
            ContactItem(
                label = "手机",
                content = "+86 ni cai",
                icon = phoneIcon // 使用自定义手机图标
            )

            Spacer(modifier = Modifier.height(10.dp))

            // 邮箱 - 使用自定义邮箱图标
            ContactItem(
                label = "邮箱",
                content = "bao mi@ni cai.com",
                icon = emailIcon // 使用自定义邮箱图标
            )

            Spacer(modifier = Modifier.height(10.dp))

            // 网名 - 使用自定义用户图标
            ContactItem(
                label = "网名",
                content = "微风竹吟",
                icon = usernameIcon // 使用自定义用户图标
            )
        }
    }
}

@Composable
fun ContactItem(label: String, content: String, icon: Painter) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.fillMaxWidth()
    ) {
        // 使用Image组件显示自定义图标，可以更好地控制显示效果
        Image(
            painter = icon,
            contentDescription = "$label 图标",
            modifier = Modifier.size(32.dp)
        )

        Spacer(modifier = Modifier.width(16.dp))

        Column(
            horizontalAlignment = Alignment.Start
        ) {
            Text(
                text = label,
                fontSize = 14.sp,
                color = Color.Black.copy(alpha = 0.8f) // 调整颜色确保在渐变背景上可读
            )
            Text(
                text = content,
                fontSize = 18.sp,
                color = Color.Black, // 确保文字可读性
                modifier = Modifier.padding(top = 4.dp)
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        GradientBackgroundWithContent()
    }
}